package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.pojo.China;
import com.woniu.service.AddressService;
import com.woniu.service.ChinaService;
import com.woniu.util.JwtUtils;
import com.woniu.vo.AddressRequest;
import com.woniu.vo.AddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "收货地址管理")
@Slf4j
public class AddressController {
    @Autowired
    private AddressService addressService;

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private ChinaService chinaService;

    /**
     * 通过用户id获取收货地址
     */
    @GetMapping("/findAddress")
    @ApiOperation("获取当前用户收货地址")
    public ResponseEntity<List<AddressVo>> findAddress(@RequestHeader(name = "Authorization", required = false) String token) {
        // 获取当前登录用户 token
        String userId = jwtUtils.getValue(token, "userId");
        List<AddressVo> addressVoList = addressService.findAddressByUserId(Long.valueOf(userId));
        return ResponseEntity.ok(addressVoList);
    }

    /**
     * 获取所有省份
     * */
    @GetMapping("/getAllProvince")
    @ApiOperation("获取所有省份")
    public ResponseEntity<List<China>> getAllProvinces(){
        log.info("获取所有省份");
        List<China> chinaList = addressService.getAllProvinces();
        return ResponseEntity.ok(chinaList);
    }

    /**
     * 根据省份 id，查询该省所有市
     * */
    @GetMapping("/getAllCities")
    @ApiOperation(value = "根据省份id查询市")
    @ApiImplicitParam(name = "provinceId", value = "省份id", required = true, dataType = "Integer") // 该参数在接口文档中显示
    public ResponseEntity<List<China>> getAllCities(Integer provinceId){
        List<China> list = addressService.getAllCities(provinceId);
        return ResponseEntity.ok(list);
    }

    /**
     * 保存用户地址
     *
     * */
    @PostMapping("/save")
    @ApiOperation("保存收货地址")
    public ResponseEntity saveAddress(@RequestBody AddressRequest addressRequest,
                                      @RequestHeader(name = "Authorization", required = false) String token){
        // 获取用户登录 id
        String userId = jwtUtils.getValue(token, "userId");
        addressService.saveAddress(addressRequest, Long.valueOf(userId));
        return ResponseEntity.ok().build();
    }



}
